<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    function Person(name = "张三", age = 20) {
        this.name = name;
        this.age = age;
        this.hubby = ["java", "c++"];
        console.log("Person");
    }

    Person.prototype.say = function() {
        console.log(this.name);
    }

    function Student(name, age, score) {
        // this.name = name;
        // this.age = age;
        this.score = score;
    }

    //原型链继承
    Student.prototype = new Person();
    let s1 = new Student("s1", 10, 100);
    console.log(s1);
    console.log(s1.age);
    console.log(s1.name);
    s1.hubby.push("mysql");
    s1.say();
    let s2 = new Student("s1", 10, 100);
    console.log(s2);
    console.log(s2 instanceof Student);
    console.log(s2 instanceof Person);

    console.log("-------------------Teacher---------------------");
    //盗用构造函数继承
    function Teacher(name, age, major) {
        // this.name = name;
        // this.age = age;
        //盗用构造函数
        // Person(name, age);
        //本质上就是把Person当成一个普通函数来调用，关键点在this的传递
        Person.call(this, name, age);//传递this
        // aa.call(this, name, age);
        this.major = major;
    }

    // function aa(name, age) {
    //     this.name = name;
    //     this.age = age;
    // }

    let t1 = new Teacher("t1", 40, "javascript");
    console.log(t1);
    console.log(t1.age);
    console.log(t1.name);
    // t1.hubby.push("mysql");
    // t1.say();//方法继承不到
    let t2 = new Teacher("t2", 30, "java");
    console.log(t2);
    console.log(t2 instanceof Teacher);
    console.log(t2 instanceof Person);

    console.log("-------------------Work---------------------");
    //组合继承
    function Work(name, age, salary) {
        // this.name = name;
        // this.age = age;
        Person.call(this, name, age);//传递this
        this.salary = salary;
    }
    Work.prototype = new Person();
    let w1 = new Work("w1", 40, 4000);
    console.log(w1);
    console.log(w1.age);
    console.log(w1.name);
    w1.hubby.push("mysql");
    w1.say();//方法继承不到
    let w2 = new Work("w2", 30, 10000);
    console.log(w2);
    console.log(w2 instanceof Work);
    console.log(w2 instanceof Person);

    console.log("-------------------Work---------------------");
    //Object.create方法，原型式继承
    let c1 = Object.create(new Person("c1", 50));
    console.log(c1);
    console.log(c1 instanceof Person);
    let c2 = Object.create(new Person("c2", 60), {
        "game": {
            value: ["LOL", "Dota2", "绝地求生"]
        }
    });
    console.log(c2);
    console.log(c2 instanceof Person);
    //配合Object.assign方法使用
    let obj = {
        salary: 1000
    }
    console.log(Object.assign(c2, obj));


</script>
</body>
</html>